﻿@inherits AdminCompontentBase
@page "/alarmRules"

<PageTitle>@T("AlarmRule")</PageTitle>
<AutoHeight Overflow="!_showEmptyPlaceholder" AutoClass="mt-6">
    <HeaderContent>
        <SSearchPanel @bind-Value="@_queryParam.Filter" Advanced OnEnter="RefreshAsync" OnClearClick="HandleClearAsync" OnClick="HandleAdd">
            <ChildContent>
                <SLabeledRadioGroup @bind-Value="@_queryParam.Type" ActiveClass="fill-background" Class="white" Dense OnChange="HandleTypeChange">
                    @foreach (var item in Enum.GetValues<AlarmRuleTypes>())
                    {
                        <SLabeledRadio Value="item" Class="px-6">
                            <span class="@(_queryParam.Type == item ? "subtitle2 emphasis2--text" : "btn regular2--text")">@T(item.ToString())</span>
                        </SLabeledRadio>
                    }
                </SLabeledRadioGroup>
            </ChildContent>
            <AdvancedContent>
                <MCol Md="2" Class="d-inline-flex">
                    <SSelect Small @bind-Value="@_queryParam.TimeType"
                             Items="Enum.GetValues<AlarmRuleSearchTimeTypes>().ToList()"
                             Label="@T(nameof(_queryParam.TimeType))"
                             ItemText="e => T(e == default ? string.Empty : e.ToString())"
                             ItemValue="e => e"
                             BackgroundColor="white"
                             Clearable
                             TItem="AlarmRuleSearchTimeTypes"
                             TItemValue="AlarmRuleSearchTimeTypes"
                             TValue="AlarmRuleSearchTimeTypes"
                             Class="rounded-lg body2"
                             OnSelectedItemUpdate="RefreshAsync"
                             OnClearClick="RefreshAsync">
                    </SSelect>
                </MCol>
                <SDateTimeRangeToolbar Class="ml-6" StartDateTime="_queryParam.StartTime" EndDateTime="_queryParam.EndTime" OnUpdate="DateRangChangedAsync" ShowQuickChange="false" />
                @if (_queryParam.Type == AlarmRuleTypes.Log)
                {
                    <MCol Md="2" Class="ml-6">
                        <SSelect Small @bind-Value="@_queryParam.ProjectIdentity"
                             Items="_projectItems"
                             Label="@T(nameof(_queryParam.ProjectIdentity))"
                             ItemText="e => e.Text"
                             ItemValue="e => e.Value"
                             BackgroundColor="white"
                             Clearable
                             TItem="SelectItem<string>"
                             TItemValue="string"
                             TValue="string"
                             Class="rounded-lg body2"
                             OnSelectedItemUpdate="HandleProjectChange"
                             OnClearClick="RefreshAsync">
                        </SSelect>
                    </MCol>
                    <MCol Md="2" Class="ml-6">
                        <SSelect Small @bind-Value="@_queryParam.AppIdentity"
                             Items="_appsItems"
                             Label="@T(nameof(_queryParam.AppIdentity))"
                             ItemText="e => e.Text"
                             ItemValue="e => e.Value"
                             BackgroundColor="white"
                             Clearable
                             TItem="SelectItem<string>"
                             TItemValue="string"
                             TValue="string"
                             Class="rounded-lg body2"
                             OnSelectedItemUpdate="RefreshAsync"
                             OnClearClick="RefreshAsync">
                        </SSelect>
                    </MCol>
                }
                @if (_queryParam.Type == AlarmRuleTypes.Metric)
                {
                    <MCol Md="3" Class="ml-6">
                        <SAutoComplete Items="_metricItems" @bind-Value="_queryParam.MetricId"
                                   Label="@T("Metric")"
                                   ItemText="v => v.Text"
                                   ItemValue="v => v.Value"
                                   BackgroundColor="white"
                                   Clearable
                                   Flat
                                   Solo
                                   Small
                                   PrependInnerIcon="mdi-magnify"
                                   ClearIcon="mdi-close"
                                   Outlined="false"
                                   Class="mr-6 rounded-2 m-input--h-40"
                                   OnChange="async (string v) => await RefreshAsync()"
                                   Height="40">
                            <SelectionContent Context="data">
                                <SAutoCompleteSelection Value="data.Item.Text" Style="height:40px;" />
                            </SelectionContent>
                        </SAutoComplete>
                    </MCol>
                }
            </AdvancedContent>
        </SSearchPanel>
    </HeaderContent>
    <AutoHeightContent>
        @if (!_showEmptyPlaceholder)
        {
            <MRow NoGutters Style="height:0">
            @foreach (var _entitie in _entities.Result)
                {
                    <MCol Md="3" Class="pr-4 pb-4">
                        <AlarmRuleCard Height="_entitie.Type==AlarmRuleTypes.Metric?200:168" AlarmRule="_entitie" OnEdit="()=> HandleEdit(_entitie)" />
                    </MCol>
                }
            </MRow>
        }
        else
        {
            <SEmptyPlaceholder />
        }
    </AutoHeightContent>
    <FooterContent>
        <div class="mt-3">
            @if (!_showEmptyPlaceholder)
            {
                <SPagination Page="_queryParam.Page" PageSize="_queryParam.PageSize" Total="_entities.Total" PageChanged="HandlePageChanged" PageSizeChanged="HandlePageSizeChanged" />
            }
        </div>
    </FooterContent>
</AutoHeight>

<LogAlarmRuleUpsertModal @ref="_logUpsertModal" OnOk="HandleOk" />
<MetricAlarmRuleUpsertModal @ref="_metricUpsertModal" OnOk="HandleOk" />